



//
//  NIndustryReportFormViewController.m
//  nncloud
//
//  Created by macfai on 2018/11/19.
//  Copyright © 2018 macfai. All rights reserved.
//

#import "NIndustryReportFormViewController.h"
#import "NFeedbackCell.h"
#import "NTianBaoDetailViewController.h"
#import "NIndustyBaobiaoModel.h"
#import "NBaobiaoCell.h"
#import "NBaobiaoSearchVC.h"

@interface NIndustryReportFormViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)UITableView *tableView;

@property (nonatomic,strong)NSMutableArray *dataArray;

@property (nonatomic,strong)NSDictionary *paraDict;
@property (nonatomic,assign)NSInteger currentPage;

@end



@implementation NIndustryReportFormViewController

#define cellIdentifier  @"abaobiao"
#define HeaderHight  290
#define PageSize 20

-(UITableView *)tableView{
    //头尾刷新
    if (!_tableView) {
        WeakStament(ws);
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 0, ScreenWidth , ScreenHeight - 64) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 106.f;
        // _tableView.separatorColor = [UIColor clearColor];
        _tableView.backgroundColor = kBackgroundColor;
        [_tableView registerNib:[UINib nibWithNibName:@"NBaobiaoCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
//        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//            [ws loadData];
//        }];
//         _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//            [ws loadMoreData];
//        }];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kWhiteColor;
//    self.currentPage = 0;
    if ([_userType isEqualToString:@"2"]) {
        //政府端
         [self.navigationItem setItemWithTitle:@"工业报表" textColor:kNavigationTitleColor fontSize:18 itemType:center];
         [self addNavgationRightButtonWithFrame:CGRectMake(0, 0, 40, 40) title:@"" Image:@"搜索" selectedIMG:nil tartget:self action:@selector(search)];
    }else{
        //企业端
         [self.navigationItem setItemWithTitle:@"我的工业报表" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    }
   
    self.dataArray = [NSMutableArray array];
    [self.view addSubview:self.tableView];
    [self loadData];
    // Do any additional setup after loading the view from its nib.
}
- (void)search{
    WeakStament(ws);
    //采用present方式进行搜索
    NBaobiaoSearchVC *search = [NBaobiaoSearchVC new];
    //block传值
    search.paraBlock = ^(NSDictionary * _Nonnull dict) {
        YYLog(@"%@",dict);
        //拿到这个值进行筛选
        self.paraDict = dict;
        [ws loadData];
    };
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:search];
    [self presentViewController:nav animated:YES completion:^{
    }];
//    [self.navigationController pushViewController:search animated:YES];
}

- (void)loadData{

    self.currentPage = 1;
    NSDictionary *dic;
    NSString *url;
    if ([_userType isEqualToString:@"1"]) {
        ///企业端
        dic = @{@"userId":[NUserModel shareInstance].userInfoId?:@""};
        url = kIndustryReportListUrl;
        
    }else{
        
        //政府端进行搜索 字典进行拼接
//        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
//        dict[@"size"] = @100;
//        dict[@"curPage"] = @(self.currentPage);
//        dict[@"startTime"] = self.paraDict[@"startTime"];
//        dict[@"endTime"] = self.paraDict[@"endTime"];
        
        dic = self.paraDict?:@{};//政府端首次进来没有值
        url = kGovIndustryReportUrl;
    
    }
    
    [self.dataArray removeAllObjects];
    [[NNRCNetRequest manager]getWithUrl:url params:dic success:^(id result) {
        YYLog(@"%@",result);
        WeakStament(ws);
        if (self.currentPage==1) {
            [self.dataArray  removeAllObjects];
        }
        if ([[result objectForKey:@"code"] integerValue] == 0) {

            NSArray *arr = result[@"page"][@"records"];
            YYLog(@"%@",arr);
            for (NSDictionary *dict in arr) {
                NIndustyBaobiaoModel *model = [NIndustyBaobiaoModel mj_objectWithKeyValues:dict];
                [ws.dataArray addObject:model];
            }
            [ws.tableView reloadData];
            [ws endRefresh];
        }else{
            [AFToast showText:result[@"msg"]];
            [ws endRefresh];
        }
        
    } failure:^(id failure) {
        
    }];
}
- (void)endRefresh{
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}

#pragma mark - delegate datasource

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NIndustyBaobiaoModel *model = self.dataArray[indexPath.row];
    NTianBaoDetailViewController *detail = [NTianBaoDetailViewController new];
    detail.hidesBottomBarWhenPushed = YES;
    if (model.industryId) {
        detail.baobiaoId = model.industryId;
        detail.qiyeName = model.enterpriseName;
    }
    detail.atitle = @"工业报表详情";
    
    [self.navigationController pushViewController:detail animated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    NBaobiaoCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    NIndustyBaobiaoModel *model = self.dataArray[indexPath.row];
    cell.industryBaobiaoModel = model;
//    cell.tipLabel.hidden = YES;
//    if (self.dataArray.count>indexPath.row) {
//
//    }
    return cell;
}

@end
